<?php
define('MY_PATH', dirname(dirname(__FILE__)) . '/');

include_once MY_PATH.'application/configs/db.php';
//appsfly的数据备份    http://***.***.***.***/master/mcGM/tmp_process/africaappsfly.php
// http://52.50.212.140/master/mcGM/tmp_process/africaappsfly.php
//接收数据并保存

// 接收数据
$request = file_get_contents ( "php://input" );
mylog ( "AppsflyData", $request);
$row = json_decode ( $request, true );

//连接数据库
$connInfo = $config['log_db'];
mysql_connect($connInfo['host'], $connInfo['user'], $connInfo['password']) or die(mysql_errno());
mysql_select_db($config['log_db']['name']) or die(mysql_errno());

// 存表
$row['operationtime'] = date('Y-m-d H:i:s', time());
if ($row ['platform'] == 'ios') {
	// 存ios表 AppsflyDataIOS
	$sql = "INSERT INTO AppsflyDataIOS(device_type,download_time,click_time,agency,ip,cost_per_install,is_retargeting,app_name,re_targeting_conversion_type,city
,af_sub1,idfv,af_sub2,event_value_af_revenue,event_value_af_currency,event_value_af_content_id,event_value_af_content_type,af_sub3,af_sub4,customer_user_id
,mac,af_sub5,campaign,event_name,currency,install_time,event_time,platform,sdk_version,appsflyer_device_id,device_name,wifi,media_source,country_code,http_referrer,idfa,
click_url,language,app_id,app_version,attribution_type,af_siteid,os_version,event_type,af_ad,af_ad_id,af_ad_type,af_adset,af_adset_id,af_c_id,af_channel,af_cost_currency,
			af_cost_model,af_cost_value,af_keywords,operationtime) VALUES('".$row['device_type']."','".$row['download_time']."','".$row['click_time']."','".$row['agency']."','".
	$row['ip']."','".$row['cost_per_install']."','".$row['is_retargeting']."','".$row['app_name']."','".$row['re_targeting_conversion_type']."','".$row['city']."','".$row['af_sub1']."','".
	$row['idfv']."','".$row['af_sub2']."','".$row['event_value']['af_revenue']."','".$row['event_value']['af_currency']."','".$row['event_value']['af_content_id']."','".$row['event_value']['af_content_type']."','".$row['af_sub3']."','".
	$row['af_sub4']."','".$row['customer_user_id']."','".$row['mac']."','".$row['af_sub5']."','".$row['campaign']."','".$row['event_name']."','".$row['currency']."','".
	$row['install_time']."','".$row['event_time']."','".$row['platform']."','".$row['sdk_version']."','".$row['appsflyer_device_id']."','".$row['device_name']."','".$row['wifi']."','".
	$row['media_source']."','".$row['country_code']."','".$row['http_referrer']."','".$row['idfa']."','".$row['click_url']."','".$row['language']."','".$row['app_id']."','".
	$row['app_version']."','".$row['attribution_type']."','".$row['af_siteid']."','".$row['os_version']."','".$row['event_type']."','".$row['af_ad']."','".$row['af_ad_id']."','".
	$row['af_ad_type']."','".$row['af_adset']."','".$row['af_adset_id']."','".$row['af_c_id']."','".$row['af_channel']."','".$row['af_cost_currency']."','".$row['af_cost_model']."','".
	$row['af_cost_value']."','".$row['af_keywords']."','".$row['operationtime']."')";
	
	$res = mysql_query($sql);
	if ($res == FALSE) {
		mylog ( "AppsflyData", "AppsflyData Android info insert into AppsflyDataAndroid fail !!! row=" . $row );
		echo "fail";
	} else {
		echo "success";
	}
} elseif ($row ['platform'] == 'android') {
	// 存android表 AppsflyDataAndroid
	$res = $tmpConn->insert ( "AppsflyDataAndroid", $row );
	$sql = "INSERT INTO AppsflyDataIOS(device_model,download_time,operator,click_time,agency,ip,cost_per_install,imei,is_retargeting,app_name,re_targeting_conversion_type,android_id,city
,af_sub1,af_sub2,event_value,af_sub3,af_sub4,customer_user_id
,mac,af_sub5,campaign,event_name,currency,install_time,event_time,platform,sdk_version,appsflyer_device_id,wifi,advertising_id,media_source,country_code,http_referrer,
click_url,carrier,language,app_id,app_version,attribution_type,af_siteid,os_version,device_type,event_type,af_ad,af_ad_id,af_ad_type,af_adset,af_adset_id,af_c_id,af_channel,af_cost_currency,
		af_cost_model,af_cost_value,af_keywords,operationtime) VALUES('".$row['device_model']."','".$row['download_time']."','".$row['operator']."','".$row['click_time']."','".$row['agency']."','".
	$row['ip']."','".$row['cost_per_install']."','".$row['imei']."','".$row['is_retargeting']."','".$row['app_name']."','".$row['re_targeting_conversion_type']."','".$row['android_id']."','".$row['city']."','".$row['af_sub1']."','".
    $row['af_sub2']."','".$row['event_value']."','".$row['af_sub3']."','".
	$row['af_sub4']."','".$row['customer_user_id']."','".$row['mac']."','".$row['af_sub5']."','".$row['campaign']."','".$row['event_name']."','".$row['currency']."','".
	$row['install_time']."','".$row['event_time']."','".$row['platform']."','".$row['sdk_version']."','".$row['appsflyer_device_id']."','".$row['wifi']."','".$row['advertising_id']."','".
	$row['media_source']."','".$row['country_code']."','".$row['http_referrer']."','".$row['click_url']."','".$row['carrier']."','".$row['language']."','".$row['app_id']."','".
	$row['app_version']."','".$row['attribution_type']."','".$row['af_siteid']."','".$row['os_version']."','".$row['device_brand']."','".$row['event_type']."','".$row['af_ad']."','".$row['af_ad_id']."','".
	$row['af_ad_type']."','".$row['af_adset']."','".$row['af_adset_id']."','".$row['af_c_id']."','".$row['af_channel']."','".$row['af_cost_currency']."','".$row['af_cost_model']."','".
	$row['af_cost_value']."','".$row['af_keywords']."','".$row['operationtime']."')";
	$res = mysql_query($sql);
	if ($res == FALSE) {
		mylog ( "AppsflyData", "AppsflyData Android info insert into AppsflyDataAndroid fail !!! row=" . $row );
		echo "fail";
	} else {
		echo "success";
	}
}

function mylog($folderName, $data) {
	if (is_array($data)) {
		$log = date("Y-m-d H:i:s") . " " . http_build_query($data) . "\r\n";
		//$log = date("Y-m-d H:i:s") . " device_type=" . $data['device_type']." device_id=". $data['device_id']." device_mei=".
		//$data['device_mei']." device_mac=".$data['device_mac']." installtime=".$data['installtime']. "\r\n";
	} else {
		$log = date("Y-m-d H:i:s") . " " . $data . "\r\n";
	}
	$path = "tmplog/{$folderName}/";
	createDir($path);
	$path = $path . date("Y-m-d") . ".txt";
	file_put_contents($path, $log, FILE_APPEND);
}

function createDir($path) {
	if (!file_exists($path)) {
		createDir(dirname($path));
		@mkdir($path, 0777);
	}
}
